﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DBLayer;

namespace BALayer
{
    public class GiaoDich
    {
        private DAL db = null;

        public GiaoDich()
        {
            db = new DAL();
        }

        public DataSet DanhSachBanHang()
        {
            // mahoadon, makhachhang, tenkhachhang, ngayban, mahang, tenhang, soluong, dongia
            return db.ExecuteQueryDataSet("uspDanhSachBanHang", CommandType.StoredProcedure, null);
        }

        public DataSet DanhSachHoaDon()
        {
            // mahoadon, makhachhang, hoten, ngayban
            return db.ExecuteQueryDataSet("uspDanhSachHoaDon", CommandType.StoredProcedure, null);
        }

        public DataSet ChiTietHoaDon(int mahoadon)
        {
            // mahang, tenhang, soluong, dongia
            return db.ExecuteQueryDataSet("uspChiTietHoaDon", CommandType.StoredProcedure,
                new SqlParameter("@Mahoadon", mahoadon));
        }

        public int TaoHoaDon(ref string error, int makhachhang, DateTime ngayban)
        {
            int mahoadon = -1;
            SqlParameter mhd = new SqlParameter("@Mahoadon", SqlDbType.Int);
            mhd.Direction = ParameterDirection.Output;

            if(db.ExecuteNonQuery("uspTaoHoaDon", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Id", makhachhang),
                new SqlParameter("@Ngayban", ngayban),
                mhd))
            {
                mahoadon = Convert.ToInt32(mhd.Value);
            }

            return mahoadon;
        }       

        public bool ThemHangVaoHoaDon(ref string error,int mahoadon,int mahang,int soluong,float dongia)
        {
            return db.ExecuteNonQuery("uspThemHangVaoHoaDon", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Mahoadon", mahoadon),
                new SqlParameter("@Mahang", mahang),
                new SqlParameter("@Soluong", soluong),
                new SqlParameter("@Dongia", dongia));
        }

        public DataSet DanhSachDatHang()
        {
            // madathang, mancc, tenncc, ngaydat
            return db.ExecuteQueryDataSet("uspDanhSachDatHang", CommandType.StoredProcedure, null);
        }

        public DataSet ChiTietDatHang(int madathang)
        {
            // mahang, tenhang, soluong
            return db.ExecuteQueryDataSet("uspChiTietDatHang", CommandType.StoredProcedure, new SqlParameter("@Madathang", madathang));
        }

        public int TaoDonHang(ref string error,int mancc,DateTime ngaydat)
        {
            SqlParameter madh = new SqlParameter("@Madonhang", DbType.Int32);
            madh.Direction = ParameterDirection.Output;

            int madonhang = -1;

            if(db.ExecuteNonQuery("uspTaoDonHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Mancc", mancc),
                new SqlParameter("@Ngaydat", ngaydat),
                madh))
            {
                madonhang = Convert.ToInt32(madh.Value);
            }

            return madonhang;
        }

        public bool ThemVaoDonHang(ref string error,int madathang,int mahang,int soluong)
        {
            return db.ExecuteNonQuery("uspThemVaoDonHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Madonhang", madathang),
                new SqlParameter("@Mahang", mahang),
                new SqlParameter("@Soluong",soluong));
        }

        public DataSet DanhSachNhapHang()
        {
            // manhaphang, madathang, mancc, tencc, ngaynhap
            return db.ExecuteQueryDataSet("uspDanhSachNhapHang", CommandType.StoredProcedure, null);
        }

        public DataSet ChiTietNhapHang(int manhaphang)
        {
            // mahang, tenhang, dongia, hangdung
            return db.ExecuteQueryDataSet("uspChiTietNhapHang", CommandType.StoredProcedure,
                new SqlParameter("@Manhaphang", manhaphang));
        }

        public DataSet DanhSachDonHangChuaHoanTat()
        {
            // madathang, mancc, tenncc, ngaydat
            return db.ExecuteQueryDataSet("uspDonHangChuaHoanTat", CommandType.StoredProcedure, null);
        }

        public int NhapHang(ref string error,int madonhang,DateTime ngaynhap)
        {
            int manhap = -1;
            SqlParameter r = new SqlParameter("@Manhaphang", DbType.Int32);

            if(db.ExecuteNonQuery("uspNhaphang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Madonhang", madonhang),
                new SqlParameter("@Ngaynhap", ngaynhap),
                r))
            {
                manhap = Convert.ToInt32(r.Value);
            }

            return manhap;
        }

        public bool ThemChiTietNhapHang(ref string error,int manhaphang,int mahang,float dongia, DateTime handung)
        {
            return db.ExecuteNonQuery("uspThemChiTietNhapHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Manhaphang", manhaphang),
                new SqlParameter("@Mahang", mahang),
                new SqlParameter("@Dongia", dongia),
                new SqlParameter("@Handung", handung));
        }

        public bool ThemChiTietHangHoa(ref string error,int manhap,int mahang,int soluong,float dongia,DateTime handung)
        {
            return db.ExecuteNonQuery("uspThemChiTietHangHoa", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Manhap", manhap),
                new SqlParameter("@Mahang", mahang),
                new SqlParameter("@Soluong", soluong),
                new SqlParameter("@Dongia", dongia),
                new SqlParameter("@Handung", handung));
        }

        public bool CapNhatDonGia(ref string error,int mahang,float dongia)
        {
            return db.ExecuteNonQuery("uspCapNhatDonGia", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Mahang", mahang),
                new SqlParameter("@Dongia", dongia));
        }

        public DataSet ChiTietDonHang(int madonhang)
        {
            // mahang, tenhang, soluong
            return db.ExecuteQueryDataSet("uspChiTietDonHang", CommandType.StoredProcedure,
                new SqlParameter("@Madonhang", madonhang));
        }

        public DataSet DanhSachTraHang()
        {
            // matrahang, mancc, tenncc, ngaytra, mahang, tenhang, soluong, lydo
            return db.ExecuteQueryDataSet("uspDanhSachTraHang", CommandType.StoredProcedure, null);
        }

        public int TaoDonTraHang(ref string error,int mancc,DateTime ngaytra)
        {
            int matrahang = -1;
            SqlParameter math = new SqlParameter("@Matrahang", DbType.Int32);

            if(db.ExecuteNonQuery("uspTaoDonTraHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Mancc", mancc),
                new SqlParameter("@Ngaytra", ngaytra),
                math))
            {
                matrahang = Convert.ToInt32(math.Value.ToString());
            }

            return matrahang;
        }

        public bool ThemHangVaoDonTraHang(ref string error,int matrahang,int manhaphang,int mahang,int soluong)
        {
            return db.ExecuteNonQuery("uspThemHangVaoDonTraHang", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Matrahang", matrahang),
                new SqlParameter("@Manhaphang", manhaphang),
                new SqlParameter("@Mahang", mahang),
                new SqlParameter("@Soluong", soluong));
        }

        public void BeginTransaction()
        {
            db.BeginTransaction();
        }

        public void CommitTransaction()
        {
            db.CommitTransaction();
        }

        public void RollbackTransaction()
        {
            db.RollbackTransaction();
        }
    }
}
